package com.elink.enginerring.news;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.elink.enginerring.category.entity.SecondaryCategory;
import com.elink.enginerring.category.service.ICategoryService;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.util.JSONUtil;

/**
 * 新闻资讯分类
 * @author hxjxiaojun
 * @time 2016-9-20
 *
 */
@RestController
@RequestMapping("news/type")
public class NewsCategoryController {
	@Autowired
	private ICategoryService  categoryService;

	/**
	 * 获取分类
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Map<String, Object> getList() {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getSecondaryCategory(8)) ;
//		return newsTypeService.getList();
	}

	/**
	 * 添加分类
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(@ModelAttribute @Valid SecondaryCategory sc) {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
//		newsTypeService.add(type);
		categoryService.addSecond(sc);
		return JSONUtil.getJSON(ErrorType.SUCCESS) ;
	}
	
	/**
	 * 修改分类
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public Map<String, Object> modify(@ModelAttribute @Valid SecondaryCategory sc) {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
//		newsTypeService.modify(type);
		categoryService.modifySecond(sc);
		return JSONUtil.getJSON(ErrorType.SUCCESS) ;
	}
}
